package com.liyu.iblog.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.google.appengine.api.datastore.Key;

@SuppressWarnings("serial")
@Entity
public class Blog implements DomainObject {
    
    @Id
    private Key key;
    
    /** 
     * hyphen concatenated title ( will be part of the 
     * permanent link, should not be changed after creating)
     */
    @Column(nullable = false)
    private String id;
    
    @Column(nullable = false)
    private String title;
    
    //store the summary of the blog content
    @Column(length = 500)
    private String summary;
    
    @Lob
    //@Basic(fetch = FetchType.LAZY)
    private String content;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpateTime;
    
    private Integer commentCount;
    
//    @ManyToOne(fetch = FetchType.LAZY)
//    private SysUser author;
    
    //the key of the publisher
    private String userKey;

    //the key of the category the blog belongs to
    private String categoryKey;
    
    private String tagsString;
    
    //the list of the tag key
    @Basic(fetch = FetchType.EAGER)
    private List<String> tags = new ArrayList<String>();
    
    @OneToMany(mappedBy = "blog", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Comment> comments = new ArrayList<Comment>();
    
    public Blog() {
        
    }

    /**
     * prepersist process: escape the html tag
     * set the createTime with current system time
     * create the summary of the blog content  
     */
    @PrePersist
    public void executePrePresistProcess() {
        //escape html
        //content = HtmlUtils.htmlEscape(content);
        //generate last update time
        lastUpateTime = new Date();
        if (createTime == null) {
            createTime = new Date();  
            if (commentCount == null)
                commentCount = 0;
        } 
        
    }
    
    @PostLoad
    public void executePostLoadProcess() {
        //content = HtmlUtils.htmlUnescape(content);
        //prepopulate the comment size
    }
    
    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastUpateTime() {
        return lastUpateTime;
    }

    public void setLastUpateTime(Date lastUpateTime) {
        this.lastUpateTime = lastUpateTime;
    }

    public Integer getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(Integer commentCount) {
        this.commentCount = commentCount;
    }
    
//    public SysUser getAuthor() {
//        return author;
//    }
//
//    public void setAuthor(SysUser author) {
//        this.author = author;
//    }

    public String getUserKey() {
        return userKey;
    }

    public void setUserKey(String userKey) {
        this.userKey = userKey;
    }
    
    public String getCategoryKey() {
        return categoryKey;
    }

    public void setCategoryKey(String categoryKey) {
        this.categoryKey = categoryKey;
    }

    public List<String> getTags() {
        return tags;
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }
    
    public void addComment(Comment comment) {
        comment.setBlog(this);
        comments.add(comment);
    }

    public String getTagsString() {
        return tagsString;
    }

    public void setTagsString(String tagsString) {
        this.tagsString = tagsString;
    }
    
    public void addTag(String tag) {
        tags.add(tag);
    }
    
    public void clearTags() {
        this.tags.clear();
    }
}
